package com.事务;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class TestTX {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.199.130", 6379);
        jedis.flushDB();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("age", "6");
        jsonObject.put("name", "wmw");
        String s = jsonObject.toJSONString();
        //开启事务
        Transaction multi = jedis.multi();
        try {
            //开启事务之后通过事务来操作
            multi.set("user", s);
            int i=1/0;//运行时异常
            multi.exec();
            System.out.println(jedis.get("user"));
        }catch (Exception e){
            //若存在异常则放弃事务
            multi.discard();
            e.printStackTrace();
        }
        finally {
            jedis.close();
        }


    }
}
